﻿using System.Threading;
using System;
using System.Windows;
using Prism.Ioc;
using Prism.Regions;
using YJTMedicalAideManage.Cores.Events;
using YJTMedicalAideManage.Cores.Globals;
using YJTMedicalAideManage.Cores.Servers;
using YJTMedicalAideManage.ViewModels;
using HandyControl.Collections;

namespace YJTMedicalAideManage.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;
        private readonly ApiService _apiService;
        public MainWindow(IContainerExtension container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
            InitializeComponent();
            var viewmodel = new MainWindowViewModel();
            CloseStringEvent.Instance.Subscribe(OnLoginSuccess);
            DataContext = viewmodel;
            _apiService = new ApiService();
            LoginVisibilityEvent.Instance.Subscribe(LoginCommandMethod);
        }
        private void LoginCommandMethod(Visibility visibility)
        {
            if (visibility == Visibility.Collapsed)
            {
                GlobalLoadingMask.Visibility = Visibility.Collapsed;
            }
            else
            {
                GlobalLoadingMask.Visibility = Visibility.Visible;
            }
        }
        private bool islogin = false;
        private void OnLoginSuccess(string v)
        {
            isbool = false;
            
            islogin = true;
            Close();
        }
        private bool isbool = true;
        private async void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (islogin)
            { 
                var mainWindow = _container.Resolve<LoginView>();
                RegionManager.SetRegionManager(mainWindow, _regionManager);
                mainWindow.Show();
                e.Cancel = false;
                islogin = false;
                return;
            }
            if (isbool == false)
            {
                return;
            }
            var confirmResult = MessageBox.Show("确定要退出吗？", "确认退出", MessageBoxButton.YesNo);
            isbool = false;
            if (confirmResult != MessageBoxResult.Yes)
            {
                e.Cancel = true;
                return;
            }
            try
            {
                using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                {
                    var data1 = await _apiService.logoutAsync(GlobalApp.Instance.Manager_Id);
                }
            }
            catch (OperationCanceledException)
            {
                MessageBox.Show("注销请求超时，已取消");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"注销过程中发生错误：{ex.Message}");
            }
            finally
            {
                Application.Current.Shutdown(); // 确保最终关闭应用程序
            }
        }
    }
}
